package com.educate.serviceedu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.educate.commonutils.ResultOne;
import com.educate.serviceedu.entity.EduTeacher;
import com.educate.serviceedu.entity.vo.TeacherQuery;
import com.educate.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-07-22
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    //把service注入
    @Autowired
    private EduTeacherService teacherService;


    //1查询讲师的所有数据
    //rest风格
    @ApiOperation("所有讲师列表")
    @GetMapping("findAll")
    public ResultOne findAllTeacher(){
        //调用service的方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);
        return ResultOne.ok().data("items",list);
    }
    /**
     * 逻辑删除讲师的方法
     */
    @ApiOperation("根据id删除讲师")
    @DeleteMapping("{id}")
    public ResultOne removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return ResultOne.ok();
        }else{
            return ResultOne.error();
        }
    }
    /**
     * 分页查询讲师得方法
     */
    @ApiOperation("分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public ResultOne pageListTeacher(@PathVariable long current,
                                     @PathVariable long limit){
//        创建page对象
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时,底层封装,把分页所有数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//总记录数目
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合
        return ResultOne.ok().data("total",total).data("rows",records);
    }
    /**
     * 条件查询带分页的方法
     */
    @ApiOperation("条件查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
//    @RequestMapping(path = "/pageTeacherCondition/{current}/{limit}",method = RequestMethod.POST)
    public ResultOne pageTeacherCondition(@PathVariable long current,
                                          @PathVariable long limit,
                                          @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        //构建条件
       QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过动态sql
        String name =teacherQuery.getName();
        Integer level=teacherQuery.getLevel();
        String begin=teacherQuery.getBegin();
        String end=teacherQuery.getEnd();
        //判断条件值是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
//        teacherService.page()
        teacherService.page(pageTeacher,queryWrapper);
        long total = pageTeacher.getTotal();//总记录数目
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合
        return ResultOne.ok().data("total",total).data("rows",records);
    }
    /**
     * 添加讲师的方法
     */
    @ApiOperation("增加讲师")
    @PostMapping("addTeacher")
    public ResultOne addTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return ResultOne.ok();
        }else{
            return ResultOne.error();
        }
    }
    /**
     * 根据讲师id进行查询
     */
    @ApiOperation("根据讲师id进行查询")
    @GetMapping("getTeacher/{id}")
    public ResultOne getTeacher(@PathVariable String id){
        //异常方法检验
//        try{
//            int i=0;
//            System.out.println(2/i);
//        }catch(Exception e){
//            throw new EducateException(201,"执行了自定义异常信息");
//
//        }
        EduTeacher byId = teacherService.getById(id);
        return ResultOne.ok().data("teacher",byId);
    }
    /**
     * 讲师修改功能
     */
    @ApiOperation("讲师修改功能")
    @PostMapping("updateTeacher")
    public ResultOne updateTeacher(@RequestBody EduTeacher eduTeacher){
//
        boolean b = teacherService.updateById(eduTeacher);
        if(b){
            return ResultOne.ok();
        }else {
            return ResultOne.error();
        }
    }
}

